package com.jp.knowledge.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.jp.knowledge.command.Command;
import com.jp.knowledge.command.CommandInvoker;
import com.jp.knowledge.command.CommandReceiver;
import com.jp.knowledge.model.UserData;
import com.umeng.message.IUmengCallback;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.MsgConstant;
import com.umeng.message.PushAgent;
import com.umeng.message.UTrack;
import com.umeng.message.UmengNotificationClickHandler;
import com.umeng.message.entity.UMessage;
import com.umeng.socialize.UmengTool;

import java.util.Map;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by 528787067@qq.com on 2017/4/27.
 */

public class PushUtil {

    private static PushUtil pushUtil;
    public static synchronized PushUtil getInstance(Context context){
        if(pushUtil == null) {
            pushUtil = new PushUtil();
            pushUtil.init(context.getApplicationContext());
        }
        return pushUtil;
    }

    private static PushAgent mPushAgent;
    private SharedPreferences sharedPreferences;

    private void init(Context context){
        sharedPreferences = context.getSharedPreferences("umengPush", MODE_PRIVATE);

        mPushAgent = PushAgent.getInstance(context);
        mPushAgent.register(new IUmengRegisterCallback() {
            @Override
            public void onSuccess(String deviceToken) {
                L.i("deviceToken", deviceToken);
            }
            @Override
            public void onFailure(String s, String s1) {
                L.e("onFailure",s+"====="+s1);
            }
        });
        mPushAgent.setNotificationClickHandler(new UmengNotificationClickHandler() {
            @Override
            public void dealWithCustomAction(Context context, UMessage msg) {
                try {
                    Map<String, String> msgExtra = msg.extra;
                    String command = msgExtra.get("command");
                    String commpanyId = msgExtra.get("companyId");
                    String recordId = msgExtra.get("recordId");
                    String messageId = msgExtra.get("messageId");
                    String parameterId = msgExtra.get("parameterId");

                    /* 命令接收者封装命令参数 */
                    CommandReceiver commandReceiver = new CommandReceiver.Builder(context)
                            .withCompanyId(commpanyId)
                            .withRecordId(recordId)
                            .withMessageId(messageId)
                            .withParameterId(parameterId)
                            .build();
                    /* 创建命令对象 */
                    Command commandObject = Command.create(command, commandReceiver);
                    /* 命令执行者执行命令 */
                    CommandInvoker.executeCommand(commandObject);
                } catch (Exception e){
                    L.e(e.getMessage());
                }
            }
        });
        setPushEnable(getPushEnable());                             // 推送开关
        setNoDisturbMode(getNoDisturbMode());                       // 夜间免打扰
        setNotificationPlaySound(getNotificationPlaySound());       // 声音
        setNotificationPlayLights(getNotificationPlayLights());     // 呼吸灯
        setNotificationPlayVibrate(getNotificationPlayVibrate());   // 震动
    }

    /**
     * 添加用户别名
     * @param userData
     */
    public static void addAlias(final UserData userData){
        new Thread(new Runnable() {
            @Override
            public void run() {
                mPushAgent.removeAlias(userData.getUuid(), "com.jp.knowledge", new UTrack.ICallBack() {
                    @Override
                    public void onMessage(boolean b, String s) {
                        L.e(b+"===="+s);

                        mPushAgent.addAlias(userData.getUuid(), "com.jp.knowledge", new UTrack.ICallBack() {
                            @Override
                            public void onMessage(boolean b, String s) {
                                L.e(b+"===="+s);
                            }
                        });
                    }
                });

            }
        }).start();

    }

    /* 推送开关（在Activity内调用） */
    public void setPushEnable(boolean pushEnable){
        if(pushEnable) {
            mPushAgent.enable(new IUmengCallback() {
                @Override
                public void onSuccess() {}
                @Override
                public void onFailure(String s, String s1) {}
            });
        } else{
            mPushAgent.disable(new IUmengCallback() {
                @Override
                public void onSuccess() {}
                @Override
                public void onFailure(String s, String s1) {}
            });
        }
        sharedPreferences.edit().putBoolean("pushEnable", pushEnable).apply();
    }

    /* 推送开关（在Activity内调用） */
    public boolean getPushEnable(){
        return sharedPreferences.getBoolean("pushEnable", true);
    }

    /* 夜间消息免打扰 */
    public void setNoDisturbMode(boolean isNoDistrub){
        if(isNoDistrub)
            mPushAgent.setNoDisturbMode(23, 0, 7, 0);
        else
            mPushAgent.setNoDisturbMode(0, 0, 0, 0);
        sharedPreferences.edit().putBoolean("noDisturbMode", isNoDistrub).apply();
    }

    /* 夜间消息免打扰 */
    public boolean getNoDisturbMode(){
        return sharedPreferences.getBoolean("noDisturbMode", false);
    }

    /* 声音 */
    public void setNotificationPlaySound(boolean isPlaySound){
        if(isPlaySound)
            mPushAgent.setNotificationPlaySound(MsgConstant.NOTIFICATION_PLAY_SERVER);
        else
            mPushAgent.setNotificationPlaySound(MsgConstant.NOTIFICATION_PLAY_SDK_DISABLE);
        sharedPreferences.edit().putBoolean("playSound", isPlaySound).apply();
    }

    /* 声音 */
    public boolean getNotificationPlaySound(){
        return sharedPreferences.getBoolean("playSound", true);
    }

    /* 呼吸灯 */
    public void setNotificationPlayLights(boolean isPlayLights){
        if(isPlayLights)
            mPushAgent.setNotificationPlayLights(MsgConstant.NOTIFICATION_PLAY_SERVER);
        else
            mPushAgent.setNotificationPlayLights(MsgConstant.NOTIFICATION_PLAY_SDK_DISABLE);
        sharedPreferences.edit().putBoolean("playLights", isPlayLights).apply();
    }

    /* 呼吸灯 */
    public boolean getNotificationPlayLights(){
        return sharedPreferences.getBoolean("playLights", true);
    }

    /* 震动 */
    public void setNotificationPlayVibrate(boolean isPlayVibrate){
        if(isPlayVibrate)
            mPushAgent.setNotificationPlayVibrate(MsgConstant.NOTIFICATION_PLAY_SERVER);
        else
            mPushAgent.setNotificationPlayVibrate(MsgConstant.NOTIFICATION_PLAY_SDK_DISABLE);
        sharedPreferences.edit().putBoolean("playVibrate", isPlayVibrate).apply();
    }

    /* 震动 */
    public boolean getNotificationPlayVibrate(){
        return sharedPreferences.getBoolean("playVibrate", true);
    }
}
